﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HexunMicrobloggingClientUI
{
    /// <summary>
    /// TaskStatus class for storing task status.
    /// </summary>
    class TaskStatus
    {
        #region Counter related.

        /// <summary>
        /// Current total count of task thread.
        /// </summary>
        public int curTotalCount = 0;

        /// <summary>
        /// Current success count of task thread.
        /// </summary>
        public int curSuccessCount = 0;

        /// <summary>
        /// Current failed count of task thread.
        /// </summary>
        public int curFailedCount = 0;

        /// <summary>
        /// Indicate if count the failed task or not.
        /// </summary>
        public bool countEvenIfFailed = false;

        public int finishedTaskCount
        {
            get
            {
                if (countEvenIfFailed)
                {
                    return curTotalCount;
                }

                return curSuccessCount;
            }
        }

        /// <summary>
        /// The max count number.
        /// </summary>
        public int maxCountNum = 0;

        #endregion

        /// <summary>
        /// Timer instance.
        /// </summary>
        public System.Threading.Timer timer;

        #region Account related.

        /// <summary>
        /// User name of microblogging account.
        /// </summary>
        public string userName;

        /// <summary>
        /// Password of microblogging account.
        /// </summary>
        public string password;

        /// <summary>
        /// Microblogging message content.
        /// </summary>
        public string textContent;

        /// <summary>
        /// Text type.
        /// </summary>
        public EnumTextType enumTextType;

        #endregion

        /// <summary>
        /// Index of task list.
        /// </summary>
        public int indexOfList;

        #region TaskStatusTypeChanged Event Handler

        /// <summary>
        /// Task thread status.
        /// </summary>
        private TaskStatusType curTaskStatusType = TaskStatusType.NotStarted;

        /// <summary>
        /// Store the privious TaskStatusType.
        /// </summary>
        private TaskStatusType oldTaskStatusType = TaskStatusType.NotStarted;

        /// <summary>
        /// Public property to get current task status type.
        /// </summary>
        public TaskStatusType TaskStatusType
        {
            get { return curTaskStatusType; }
        }

        /// <summary>
        /// Set new task status.
        /// </summary>
        /// <param name="newTaskStatusType">The new task status which will be set.</param>
        public void SetTaskStatus(TaskStatusType newTaskStatusType)
        {
            oldTaskStatusType = curTaskStatusType;
            curTaskStatusType = newTaskStatusType;

            // Trigger event.
            if (!curTaskStatusType.Equals(oldTaskStatusType))
            {
                TaskStatusTypeChangedEventArgs e = new TaskStatusTypeChangedEventArgs();
                OnTaskStatusTypeChanged(e);
            }
        }

        protected virtual void OnTaskStatusTypeChanged(TaskStatusTypeChangedEventArgs e)
        {
            if (taskStatusTypeChanged != null)
            {
                taskStatusTypeChanged(this, e);
            }
        }

        /// <summary>
        /// The event handler of task status type changed.
        /// </summary>
        public event TaskStatusTypeChangedEventHandler taskStatusTypeChanged;

        #endregion

        public bool successWithResponseFailed = true;
    }

    public delegate void TaskStatusTypeChangedEventHandler(object sender, TaskStatusTypeChangedEventArgs e);

    public class TaskStatusTypeChangedEventArgs : EventArgs
    {
        public TaskStatusTypeChangedEventArgs()
        {
 
        }
    }

    public enum TaskStatusType
    {
        /// <summary>
        /// The task is not started.
        /// </summary>
        NotStarted,

        /// <summary>
        /// The task has been started.
        /// </summary>
        Started,

        /// <summary>
        /// The task has been paused.
        /// </summary>
        Paused,

        /// <summary>
        /// The task has stoped.
        /// </summary>
        Stoped,

        /// <summary>
        /// The task is finished.
        /// </summary>
        Finished
    }
}
